import { useContext, useEffect, useMemo, useRef, useState } from 'react';
import { Button, Space, message, Modal } from 'antd';
import { QuestionCircleOutlined } from '@ant-design/icons';
import { WorkflowChedkRecord, WorkflowHndTaskBtn, WorkflowNoDefaultTable, WorkflowSwitchModal } from '@/components/WorkflowCommonCopy';
import { changeNumMoneyToChinese, getTenantID, getPosIDs, getUserID, downloadTemplate, printerPDF } from '@/utils';
import { ILoadMapTypeAll } from '@/common';
// import { StampContractModal } from './StampContractModal';
import { TipsErrorModalNew } from '@/components/TipsErrorModalNew';
import { CancelModal } from '@/components/CancelModal';
import { getBaseUrl } from '@/services/urlconst';
import { history } from 'umi';
import { LYrequestController } from '../common/FormatUse';
import { TKrequestController } from '../common/FormatReturn';

export const DetailInfoViewBottomBtn: React.FC<any> = ({
  detailInfo,
  position,
  handleSave,
  revokeReloadCallback,
  locationProps,
  detailAssetApplyReturnRun,
  detailAssetApplyUesRun,
  changeRevoke,
  curRes,
}) => {
  /** 申请页ref */
  const { confirm } = Modal;

  let { pageType } = locationProps?.state;

  const closeCurrent = () => {
    confirm({
      title: '关闭',
      icon: <QuestionCircleOutlined />,
      content: '确定要关闭当前页吗？',
      okText: '确定',
      okType: 'primary',
      cancelText: '取消',
      onOk() {
        history.push({
          pathname: locationProps?.state?.returnPathName,
          state: {
            delRouterNameSource: history.location.pathname, //必传,要删除的路由名称
            returnPathName: locationProps?.state?.returnPathName,
          },
        });
      },
      onCancel() {},
    });
  };

  return (
    <>
      <div className="tabsBodyBottomBtn">
        <Space>
          {
            // 点击新增进入
            ((!detailInfo?.id && curRes?.['/assetApplyUes/add'] && curRes?.['/assetApplyReturn/add']) ||
              (detailInfo?.state == `${getTenantID()}${ILoadMapTypeAll.STORAGE_USE_STATUS}1000` &&
                !detailInfo?.checkState &&
                curRes?.['/assetApplyUes/edit']) ||
              (detailInfo?.state == `${getTenantID()}${ILoadMapTypeAll.STORAGE_RETURN_STATUS}1000` &&
                !detailInfo?.checkState &&
                curRes?.['/assetApplyReturn/edit']) ||
              // 审核推回，重新提交
              (detailInfo?.state == `${getTenantID()}${ILoadMapTypeAll.STORAGE_USE_STATUS}1001` &&
                detailInfo?.checkState == `${getTenantID()}${ILoadMapTypeAll.ALLFLOW_CHECKRESULT}1003` &&
                detailInfo?.createrID == getUserID() &&
                detailInfo?.createPositionID == getPosIDs() &&
                curRes?.['/assetApplyUes/edit']) ||
              (detailInfo?.state == `${getTenantID()}${ILoadMapTypeAll.STORAGE_RETURN_STATUS}1001` &&
                detailInfo?.checkState == `${getTenantID()}${ILoadMapTypeAll.ALLFLOW_CHECKRESULT}1003` &&
                detailInfo?.createrID == getUserID() &&
                detailInfo?.createPositionID == getPosIDs() &&
                curRes?.['/assetApplyReturn/edit'])) &&
              // ||
              // // 复核中，有权限可编辑
              // (
              //   (
              //     detailInfo?.state == `${getTenantID()}${ILoadMapTypeAll.STORAGE_USE_STATUS}1001` &&
              //     detailInfo?.checkState == `${getTenantID()}${ILoadMapTypeAll.ALLFLOW_CHECKRESULT}1006` &&
              //     curRes?.["/assetApplyUes/edit"]
              //   )
              //   ||
              //   (
              //     detailInfo?.state == `${getTenantID()}${ILoadMapTypeAll.STORAGE_RETURN_STATUS}1001` &&
              //     detailInfo?.checkState == `${getTenantID()}${ILoadMapTypeAll.ALLFLOW_CHECKRESULT}1006` &&
              //     curRes?.["/assetApplyReturn/edit"]
              //   )
              // )
              pageType != 'detail' && (
                <Button type={'primary'} onClick={() => handleSave('0')}>
                  暂存
                </Button>
              )
          }

          {detailInfo?.checkState == `${getTenantID()}${ILoadMapTypeAll.ALLFLOW_CHECKRESULT}1002` ? (
            <Button
              type={'primary'}
              onClick={() => {
                message.success('执行中，请稍后...');
                printerPDF(detailInfo?.workflowURL);
              }}
            >
              打印
            </Button>
          ) : null}

          {position == 'top' &&
          (!detailInfo?.state ||
            ((detailInfo?.state == `${getTenantID()}${ILoadMapTypeAll.STORAGE_USE_STATUS}1000` ||
              detailInfo?.state == `${getTenantID()}${ILoadMapTypeAll.STORAGE_RETURN_STATUS}1000`) &&
              !detailInfo?.checkState &&
              detailInfo?.createrID == getUserID())) ? (
            <Button
              type={'primary'}
              // icon={<CheckCircleOutlined />}
              onClick={() => handleSave('1')}
            >
              提交
            </Button>
          ) : null}

          {position == 'top' && (
            <Button danger onClick={closeCurrent}>
              关闭
            </Button>
          )}

          {/* 流程节点弹框 */}
          {position == 'top' && (
            <WorkflowHndTaskBtn
              flowDetailInfo={detailInfo}
              requestController={detailInfo?.queryType == '1' || detailInfo?.queryType == '3' ? LYrequestController() : TKrequestController()}
              reloadCallback={() => {
                if (detailInfo?.queryType == '1' || detailInfo?.queryType == '3') {
                  detailAssetApplyUesRun({ id: detailInfo?.id });
                } else {
                  detailAssetApplyReturnRun({ id: detailInfo?.id });
                }
              }}
              revokeDealNum={revokeReloadCallback}
              flowBusinessType={
                detailInfo?.queryType == '1' || detailInfo?.queryType == '3'
                  ? `${getTenantID()}${ILoadMapTypeAll.ALLFLOW_TYPE}1011`
                  : `${getTenantID()}${ILoadMapTypeAll.ALLFLOW_TYPE}1013`
              }
              isAloneSubmit={
                detailInfo?.queryType == '1' || detailInfo?.queryType == '3'
                  ? detailInfo?.checkState == `${getTenantID()}${ILoadMapTypeAll.ALLFLOW_CHECKRESULT}1003` &&
                    detailInfo?.state == `${getTenantID()}${ILoadMapTypeAll.STORAGE_USE_STATUS}1001`
                  : detailInfo?.checkState == `${getTenantID()}${ILoadMapTypeAll.ALLFLOW_CHECKRESULT}1003` &&
                    detailInfo?.state == `${getTenantID()}${ILoadMapTypeAll.STORAGE_RETURN_STATUS}1001`
              }
            />
          )}

          {
            // 退库撤销权限判断
            (detailInfo?.queryType == '2' || detailInfo?.queryType == '4') &&
              ((detailInfo?.checkState == `${getTenantID()}${ILoadMapTypeAll.ALLFLOW_CHECKRESULT}1000` &&
                detailInfo?.state == `${getTenantID()}${ILoadMapTypeAll.STORAGE_RETURN_STATUS}1001`) ||
                (detailInfo?.checkState == `${getTenantID()}${ILoadMapTypeAll.ALLFLOW_CHECKRESULT}1003` &&
                  detailInfo?.state == `${getTenantID()}${ILoadMapTypeAll.STORAGE_RETURN_STATUS}1001`)) &&
              detailInfo?.createrID == getUserID() && (
                <Button
                  type={'primary'}
                  danger
                  onClick={() => {
                    changeRevoke();
                  }}
                >
                  撤销
                </Button>
              )
          }

          {
            // 领用撤销权限判断
            (detailInfo?.queryType == '1' || detailInfo?.queryType == '3') &&
              ((detailInfo?.checkState == `${getTenantID()}${ILoadMapTypeAll.ALLFLOW_CHECKRESULT}1000` &&
                detailInfo?.state == `${getTenantID()}${ILoadMapTypeAll.STORAGE_USE_STATUS}1001`) ||
                (detailInfo?.checkState == `${getTenantID()}${ILoadMapTypeAll.ALLFLOW_CHECKRESULT}1003` &&
                  detailInfo?.state == `${getTenantID()}${ILoadMapTypeAll.STORAGE_USE_STATUS}1001`) ||
                (detailInfo?.checkState == `${getTenantID()}${ILoadMapTypeAll.ALLFLOW_CHECKRESULT}1006` &&
                  detailInfo?.state == `${getTenantID()}${ILoadMapTypeAll.STORAGE_USE_STATUS}1001`)) &&
              detailInfo?.createrID == getUserID() && (
                <Button
                  type={'primary'}
                  danger
                  onClick={() => {
                    changeRevoke();
                  }}
                >
                  撤销
                </Button>
              )
          }
        </Space>
      </div>
    </>
  );
};

{
  /* {
            (
              (
                locationProps?.state?.pageType !== 'detail' ||
                (
                  detailInfo?.state == `${getTenantID()}${ILoadMapTypeAll.STORAGE_USE_STATUS}1001` && detailInfo?.checkState == '100199021003')) &&
              detailInfo?.createrID == getUserID()) ||
              locationProps?.state?.pageType === 'add' ?
              (
                <Button
                  type={'primary'}
                  // icon={<SaveOutlined />}
                  onClick={() => handleSave('0')}
                >
                  暂存
                </Button>
              ) : null
          } */
}
